{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e522a237",
   "metadata": {},
   "source": [
    "装饰器 decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82c8dc26",
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(func):\n",
    "    def wrapper():\n",
    "        print(\"约会开始了\")\n",
    "        func()\n",
    "        print(\"明天见\")\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "941b98ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "约会开始了\n",
      "牛郎和织女看电影\n",
      "明天见\n"
     ]
    }
   ],
   "source": [
    "def my_deca():\n",
    "    print(\"牛郎和织女看电影\")\n",
    "\n",
    "f = log(my_deca)\n",
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "57bc0cf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function log.<locals>.wrapper at 0x000001B5678A2C18>\n"
     ]
    }
   ],
   "source": [
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "27d8d4a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "@log \n",
    "def my_decora():\n",
    "    print(\"牛郎和织女看电影\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fa285e43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "约会开始了\n",
      "牛郎和织女看电影\n",
      "明天见\n"
     ]
    }
   ],
   "source": [
    "my_decora()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "06f09437",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "48\n"
     ]
    }
   ],
   "source": [
    "def func_operta(func,a,b,c,d):\n",
    "    return func(a,b)*func(c,d)\n",
    "def add(a,b):\n",
    "    return a+b\n",
    "print(func_operta(add,3,5,2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d11091e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "约会开始了\n",
      "cyn和lhl看电影\n",
      "明天见\n"
     ]
    }
   ],
   "source": [
    "def log_data(func,name1,name2):\n",
    "    def wrapper():\n",
    "        print(\"约会开始了\")\n",
    "        func(name1,name2)\n",
    "        print(\"明天见\")\n",
    "    return wrapper\n",
    "def dataname(name1,name2):\n",
    "    print(f\"{name1}和{name2}看电影\")\n",
    "f=log_data(dataname,\"cyn\",\"lhl\")\n",
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "183145ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add\n",
      "name=add,doc=This is a function for add\n"
     ]
    }
   ],
   "source": [
    "def copy_properties(src):\n",
    "    def _copy(dst):\n",
    "        dst.__name__ = src.__name__\n",
    "        dst.__doc__ = src.__doc__\n",
    "        print(dst.__name__)\n",
    "        return dst\n",
    "    return _copy\n",
    "\n",
    "def logger(fn):\n",
    "    @copy_properties(fn)  # wrapper = wrapper(fn)(wrapper)\n",
    "    def wrapper(*args,**kwargs):\n",
    "        'I am wrapper'\n",
    "        print('begin')\n",
    "        x = fn(*args,**kwargs)\n",
    "        print('end')\n",
    "        return x\n",
    "    return wrapper\n",
    "@logger\n",
    "def add(x,y):\n",
    "    '''This is a function for add'''\n",
    "    return x+y\n",
    "print(\"name={},doc={}\".format(add.__name__,add.__doc__))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4510e851",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "begin\n",
      "end\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93c10d4e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
